package fmi.chesstern.ui;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public class Messages {
	private static Locale defaultLocale = Locale.getDefault();
	private static final Map<Locale, Messages> localeToMessagesInstance = new HashMap<Locale, Messages>();
	private static final String BUNDLE = "cjt.testkit.gui.messages";
	
	private final ResourceBundle messages;

	private Messages(Locale locale) {
		messages = ResourceBundle.getBundle(BUNDLE, locale);
	}

	public String getString(String key) {
		return messages.getString(key);
	}
	
	public static Messages getInstance() {
		return getInstance(defaultLocale);		
	}
	
	public static Messages getInstance(Locale locale) {
		synchronized (localeToMessagesInstance) {
			if (!localeToMessagesInstance.containsKey(locale)) {
				localeToMessagesInstance.put(locale, new Messages(locale));
			}			
		}
		
		return localeToMessagesInstance.get(locale);
	}
}
